import java.util.Arrays;

/**
 * @author LKQ
 * @date 2022/2/15 14:09
 * @description 先排序，然后比较一段相差为1的段序列长度
 */
public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] nums = {1,3,2,2,5,2,3,7};
        solution.findLHS(nums);
    }
    public int findLHS(int[] nums) {
        Arrays.sort(nums);
        int begin = 0, ans = 0 ;
        for (int end = 0; end < nums.length; end++) {
            while (nums[end] - nums[begin] > 1) {
                begin++;
            }
            if (nums[end] - nums[begin] == 1) {
                ans = Math.max(ans, end - begin + 1);
            }
        }
        return ans;
    }
}
